package com.heatup.apt.api.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.heatup.apt.api.service.DataDictService;
import com.heatup.apt.api.service.TSysPresaleTeamService;
import com.heatup.apt.common.constants.ErrorCodeConstants;
import com.heatup.apt.common.util.DateUtils;
import com.heatup.apt.common.util.JsonUtils;
import com.heatup.apt.common.util.RequestTools;
import com.heatup.apt.common.util.ResponseTools;
import com.heatup.apt.model.api.ApiResultModel;
import com.heatup.apt.model.user.UserInfo;

/**
 * 预售团队
 * @author timo
 *
 */
@Controller
@RequestMapping("/presale/user")
public class PresaleTeamController extends BaseController{

	Logger logger = LoggerFactory.getLogger(PresaleTeamController.class);
	
	@Resource
	private TSysPresaleTeamService presaleTeamService;
	@Resource
	private DataDictService dataDictService;
	
	/**
	 * 添加预售人员
	 * @param request
	 * @param httpServletResponse
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/add_presale_user")
	public String addPresaleUser(HttpServletRequest request, HttpServletResponse httpServletResponse) {
		ApiResultModel<Object> apiResultModel = new ApiResultModel<Object>();
		String callback = RequestTools.RequestString(request, "callback", "");
		String userName = RequestTools.RequestString(request, "userName", null);
		String mobile = RequestTools.RequestString(request, "mobile", null);
		UserInfo adminUserInfo = this.getUserInfo();
		UserInfo userInfo = this.getUserInfo();
		if(userInfo == null) {
			apiResultModel.setRet(apiResultModel.RET_ERROR);
			apiResultModel.setMsg(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.USER_NOT_LOGIN.getCode());
			
		}
		try {
			apiResultModel = presaleTeamService.addPresaleUser(userName, mobile, adminUserInfo);
		} catch (Exception e) {
			
			logger.error("PresaleTeamController | addPresaleUser | error:"+e.toString()+e.getCause(),e);
			apiResultModel.setRet(apiResultModel.RET_ERROR);
			apiResultModel.setMsg(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getMessage());
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getCode());
			
		}
		apiResultModel.setLogined(this.isLogined());
		return ResponseTools.CallBackResultJsonP(JsonUtils.toJsonStr(apiResultModel), callback);
	}
	
	
	@ResponseBody
	@RequestMapping("/get_presale_user_list")
	public String getPresaleUserList(HttpServletRequest request, HttpServletResponse httpServletResponse) {
		ApiResultModel<Object> apiResultModel = new ApiResultModel<Object>();
		String callback = RequestTools.RequestString(request, "callback", "");
		UserInfo userInfo = this.getUserInfo();
		
		if(userInfo == null) {
			apiResultModel.setRet(apiResultModel.RET_ERROR);
			apiResultModel.setMsg(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.USER_NOT_LOGIN.getCode());
			
		}
		
		try {
			apiResultModel = presaleTeamService.queryPresaleUserList(userInfo);
		} catch (Exception e) {

			logger.error("PresaleTeamController | addPresaleUser | error:"+e.toString()+e.getCause(),e);
			apiResultModel.setRet(apiResultModel.RET_ERROR);
			apiResultModel.setMsg(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getMessage());
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getCode());
			
		}
		apiResultModel.setLogined(this.isLogined());
		return ResponseTools.CallBackResultJsonP(JsonUtils.toJsonStr(apiResultModel), callback);
	}
	
	
	@ResponseBody
	@RequestMapping("/del_presale_user")
	public String delPresaleUser(HttpServletRequest request, HttpServletResponse httpServletResponse) {
		ApiResultModel<Object> apiResultModel = new ApiResultModel<Object>();
		String callback = RequestTools.RequestString(request, "callback", "");
		UserInfo adminUserInfo = this.getUserInfo();
		Integer id = RequestTools.RequestInt(request, "id", 0);
		
		if(adminUserInfo == null) {
			apiResultModel.setRet(apiResultModel.RET_ERROR);
			apiResultModel.setMsg(ErrorCodeConstants.Code.USER_NOT_LOGIN.getMessage());
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.USER_NOT_LOGIN.getCode());
			
		}
		
		try {
			apiResultModel = presaleTeamService.delPresalUser(adminUserInfo, id);
		} catch (Exception e) {

			logger.error("PresaleTeamController | delPresaleUser | error:"+e.toString()+e.getCause(),e);
			apiResultModel.setRet(apiResultModel.RET_ERROR);
			apiResultModel.setMsg(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getMessage());
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getCode());
			
		}
		apiResultModel.setLogined(this.isLogined());
		return ResponseTools.CallBackResultJsonP(JsonUtils.toJsonStr(apiResultModel), callback);
	}
	
	/**
	 * 获取到店预约数据列表
	 * @param request
	 * @param httpServletResponse
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/get_store_advisory_list")
	public String getStoreAdvisoryList(HttpServletRequest request, HttpServletResponse httpServletResponse) {
		ApiResultModel<Object> apiResultModel = new ApiResultModel<Object>();
		String callback = RequestTools.RequestString(request, "callback", "");
		String dateTime = RequestTools.RequestString(request, "dateTime", null);
		UserInfo adminUserInfo = this.getUserInfo();
		try {
			apiResultModel = presaleTeamService.queryStoreAdvisoryList(adminUserInfo, dateTime);
		} catch (Exception e) {
			
			logger.error("PresaleTeamController | getStoreAdvisoryList | error:"+e.toString()+e.getCause(),e);
			apiResultModel.setRet(apiResultModel.RET_ERROR);
			apiResultModel.setMsg(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getMessage());
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getCode());
			
		}
		
		apiResultModel.setLogined(this.isLogined());
		return ResponseTools.CallBackResultJsonP(JsonUtils.toJsonStr(apiResultModel), callback);
	}
	
	@ResponseBody
	@RequestMapping("/get_book_retail_begin_time")
	public String getBookRetailBeginTime(HttpServletRequest request, HttpServletResponse httpServletResponse) {
		ApiResultModel<Object> apiResultModel = new ApiResultModel<Object>();
		String callback = RequestTools.RequestString(request, "callback", "");
		Map<String,Object> resultMap = new HashMap<>();
		Date retailBeginTime = null;
		Date nowDate = DateUtils.getDateShort(new Date());
		try {
			retailBeginTime = dataDictService.getBookRetailBeginTime();
		} catch (Exception e) {
			
			logger.error("PresaleTeamController | getBookRetailBeginTime | error:"+e.toString()+e.getCause(),e);
			apiResultModel.setRet(apiResultModel.RET_ERROR);
			apiResultModel.setMsg(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getMessage());
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getCode());
			
		}
		resultMap.put("retailBeginTime", retailBeginTime);
		resultMap.put("nowDate", nowDate);
		apiResultModel.setData(resultMap);
		apiResultModel.setLogined(this.isLogined());
		return ResponseTools.CallBackResultJsonP(JsonUtils.toJsonStr(apiResultModel), callback);
	}
	
	@ResponseBody
	@RequestMapping("/get_meal_retail_begin_time")
	public String getMealRetailBeginTime(HttpServletRequest request, HttpServletResponse httpServletResponse) {
		ApiResultModel<Object> apiResultModel = new ApiResultModel<Object>();
		String callback = RequestTools.RequestString(request, "callback", "");
		Map<String,Object> resultMap = new HashMap<>();
		Date retailBeginTime = null;
		Date nowDate = DateUtils.getDateShort(new Date());
		try {
			retailBeginTime = dataDictService.getMealRetailBeginTime();
		} catch (Exception e) {
			
			logger.error("PresaleTeamController | getMealRetailBeginTime | error:"+e.toString()+e.getCause(),e);
			apiResultModel.setRet(apiResultModel.RET_ERROR);
			apiResultModel.setMsg(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getMessage());
			apiResultModel.setErrorCode(ErrorCodeConstants.Code.SYSTEM_ERROR_EXCEPTION.getCode());
			
		}
		resultMap.put("retailBeginTime", retailBeginTime);
		resultMap.put("nowDate", nowDate);
		apiResultModel.setData(resultMap);
		apiResultModel.setLogined(this.isLogined());
		return ResponseTools.CallBackResultJsonP(JsonUtils.toJsonStr(apiResultModel), callback);
	}
	
	
}
